package cc.comac.mom.controller;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cc.comac.mom.bean.QualityReject;
import cc.comac.mom.config.OSSService;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.QualityPclrTestInvoiceService;
import cc.comac.mom.bean.QualityPclrTestInvoice;
import cc.comac.mom.entity.PclrTestInvoicePrint;
import cc.comac.mom.util.BaseConnectionData;
import cc.comac.mom.util.BeanManager;
import cc.comac.mom.util.ExportExcel;
import cc.comac.mom.util.JasperHelper;
import cc.comac.mom.util.ResultBean;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/QualityPclrTestInvoice")
public class QualityPclrTestInvoiceController {
	
	@Autowired
	private QualityPclrTestInvoiceService qualityPclrTestInvoiceService;
	
	@Autowired
	private BaseConnectionData baseConnectionData;
	
	@Autowired
	private OSSService oSSService;
	
	@ApiOperation("修改实验分类")
	@RequestMapping(value = "/updateTestClassify",method = RequestMethod.POST)
	public ResultBean updateTestClassify(@RequestBody QualityPclrTestInvoice obj) {
		return qualityPclrTestInvoiceService.updateTestClassify(obj);
	}
	
	@ApiOperation("条件查询pclr实验单")
	@RequestMapping(value = "/selectByCondition",method = RequestMethod.POST)
	public ResultBean selectByCondition(@RequestBody QualityPclrTestInvoice obj) {
		return qualityPclrTestInvoiceService.selectByCondition(obj);
	}
	

	@ApiOperation("收料完成")
	@RequestMapping(value = "/receiveCompletedByTestNo",method = RequestMethod.POST)
	public ResultBean receiveCompletedByTestNo(@RequestBody QualityPclrTestInvoice obj) {
		return qualityPclrTestInvoiceService.receiveCompletedByTestNo(obj);
	}

	@ApiOperation("开始实验")
	@RequestMapping(value = "/beginTestByTestNo/{testNo}",method = RequestMethod.GET)
	public ResultBean beginTestByTestNo(@PathVariable String testNo) {
		return qualityPclrTestInvoiceService.beginTestByTestNo(testNo);
	}

	@ApiOperation("驳回按钮")
	@RequestMapping(value = "/submitReject", method = RequestMethod.POST)
	public ResultBean submitReject(@RequestBody QualityReject QualityReject) {
		ResultBean submitReject = qualityPclrTestInvoiceService.submitReject(QualityReject);
		return submitReject;
	}

	@ApiOperation("提交审核")
	@RequestMapping(value = "/submitAuditByTestNo/{testNo}",method = RequestMethod.GET)
	public ResultBean submitAuditByTestNo(@PathVariable String testNo){
		return qualityPclrTestInvoiceService.submitAuditByTestNo(testNo);
	}
	
	@ApiOperation("审核完成")
	@RequestMapping(value = "/auditCompletedByTestNo/{testNo}",method = RequestMethod.GET)
	public ResultBean auditCompletedByTestNo(@PathVariable String testNo){
		return qualityPclrTestInvoiceService.auditCompletedByTestNo(testNo);
	}
	
	@ApiOperation("通过实验编号获取实验单信息")
	@RequestMapping(value = "/getByTestNo/{testNo}",method = RequestMethod.GET)
	public ResultBean getByTestNo(@PathVariable String testNo){
		return qualityPclrTestInvoiceService.getByTestNo(testNo);
	}
	@ApiOperation("导出实验单据数据")
	@RequestMapping(value = "/toExportExcel",method = RequestMethod.POST)
	public void exportData(HttpServletRequest request, HttpServletResponse response, QualityPclrTestInvoice obj) {
		String url = qualityPclrTestInvoiceService.exportData(request, response, obj);
		String fileName = "实验单据查询数据";
		try {
			oSSService.export(response, url, fileName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation("打印pclr表单通过pclr编号")
	@RequestMapping(value = "/printPclrTestInvoice/{testNo}",method = RequestMethod.GET)
	public ResultBean printPclrTestInvoice(@PathVariable("testNo")String testNo,HttpServletRequest request,HttpServletResponse response){
		ResultBean resultBean = new ResultBean(true);
		try {
			PclrTestInvoicePrint printPclrTestInvoice = qualityPclrTestInvoiceService.printPclrTestInvoice(testNo);
			Map<String, Object> beanToMap = BeanManager.beanToMap(printPclrTestInvoice);
			//指定导出文件名称
	        String exportFilePath="pclrTestInvoice";
	        //指定模板文件
	        ClassPathResource classPathResource = new ClassPathResource("config/pclrTestInvoice.jasper");
	        File file=classPathResource.getFile();
	        Connection connection = baseConnectionData.getConnection();
	        JasperHelper.showPdf(exportFilePath, file.getPath(), request, response, beanToMap, connection);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultBean;
	}
	
	@ApiOperation("打印NDT表单通过编号")
	@RequestMapping(value = "/printNdtTestInvoice/{testNo}",method = RequestMethod.GET)
	public ResultBean printNdtTestInvoice(@PathVariable("testNo") String testNo,HttpServletRequest request,HttpServletResponse response) {
		ResultBean resultBean = new ResultBean(true);
		try {
			PclrTestInvoicePrint pclrTestInvoicePrint = qualityPclrTestInvoiceService.printNdtTestInvoice(testNo);
			Map<String, Object> beanToMap = BeanManager.beanToMap(pclrTestInvoicePrint);
			//指定导出文件名称
	        String exportFilePath="ndtTestInvoice";
	        //指定模板文件
	        ClassPathResource classPathResource = new ClassPathResource("config/ndtTestInvoice.jasper");
	        File file=classPathResource.getFile();
	        Connection connection = baseConnectionData.getConnection();
	        JasperHelper.showPdf(exportFilePath, file.getPath(), request, response, beanToMap, connection);
		} catch (Exception e) {
			resultBean.setErrorMessge(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
}
